
print("Loading Kittiko Bag Prefab...")

local assets=
{
	Asset("ANIM", "data/anim/backpack.zip"),
	Asset("ANIM", "data/anim/swap_backpack.zip"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag_7x3.tex"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag__blue.tex"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag__green.tex"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag__purple.tex"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag__red.tex"),
	Asset("IMAGE", "mods/kittiko bag/inventoryimages/kittiko_bag__yellow.tex"),
}

local function OnEquip(inst, owner) 
	owner.AnimState:OverrideSymbol("swap_body", "swap_backpack", "backpack")
	owner.AnimState:OverrideSymbol("swap_body", "swap_backpack", "swap_body")
	owner.components.inventory.overflow = inst
	inst.components.container:Open(owner)
end

local function OnUnequip(inst, owner) 
	owner.AnimState:ClearOverrideSymbol("swap_body")
	owner.AnimState:ClearOverrideSymbol("backpack")
	owner.components.inventory.overflow = nil
	inst.components.container:Close(owner)
end

local function OnOpen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
end

local function OnClose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
end

local function OnSave(inst, data)
	if inst.color then
		data.color = inst.color
	end
end

local function OnLoadPostPass(inst, data)
	if data and data.color then
		inst.SetColor(data.color)
	end
end


local function SetColor(inst, colorname)
	if inst and inst.components.inventoryitem then
		if colorname == "default" then
			inst.components.inventoryitem.imagename = "kittiko_bag"
		else
			inst.components.inventoryitem.imagename = "kittiko_bag__"..colorname
		end
	end
end

--Was 5x4, now 7x3
local slotpos = {}
for y = 0, 6 do
	--table.insert(slotpos, Vector3(-162 -150, -y*75 + 114, 0))	--New 4th col
	--table.insert(slotpos, Vector3(-162  -75, -y*75 + 114, 0))	--New 3rd col
	--table.insert(slotpos, Vector3(-162     , -y*75 + 114, 0))	--2nd col
	--table.insert(slotpos, Vector3(-162  +75, -y*75 + 114, 0))	--1st col at screen edge

	table.insert(slotpos, Vector3(-152  -75, -y*75 + 240, 0))	--New 3rd col
	table.insert(slotpos, Vector3(-152     , -y*75 + 240 ,0))	--2nd col
	table.insert(slotpos, Vector3(-152  +75, -y*75 + 240 ,0))	--1st col at screen edge

end


local function fn(Sim)

	local inst = CreateEntity()
	
	--New inst var to store color info for AnimState:SetAddColour method, used by OnLoad/Save and modmain
	inst.color = "default"

	--New inst var to provide reference to prefab specific local function in order to set color
	inst.SetColor = SetColor

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	MakeInventoryPhysics(inst)
	
	inst.AnimState:SetBank("backpack1")
	inst.AnimState:SetBuild("swap_backpack")
	--inst.AnimState:SetMultColour(tint.x,tint.y,tint.z,tintalpha or 1)
	--inst.AnimState:SetMultColour( 0, 1, 2, 1 )
	--inst.AnimState:SetAddColour(r, g, b, 0)
	--inst.AnimState:SetAddColour(0, 0, 50, 0)	--purple
	--inst.AnimState:SetAddColour(50, 0, 50, 0)	--
	inst.AnimState:PlayAnimation("anim")
	
	inst:AddComponent("inspectable")
	
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "kittiko_bag"
	inst.components.inventoryitem.cangoincontainer = false
	inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/backpack"
	
    inst:AddComponent("equippable")
	--Add support for Backpack Mod by Fontonkonbonmon
	if EQUIPSLOTS["BACK"] then
		inst.components.equippable.equipslot = EQUIPSLOTS.BACK
	else
		inst.components.equippable.equipslot = EQUIPSLOTS.BODY
	end

	inst.components.equippable:SetOnEquip( OnEquip )
	inst.components.equippable:SetOnUnequip( OnUnequip )
        inst.components.equippable.walkspeedmult = 1.2
	
	inst:AddComponent("container")
	inst.components.container:SetNumSlots(#slotpos)
	inst.components.container.widgetslotpos = slotpos
	inst.components.container.widgetpos = Vector3(700,-110,0)	--backpack screen position
	--FUTURE: Create custom animations for new container size
	--inst.components.container.widgetanimbank = "ui_kittiko_bag"
	--inst.components.container.widgetanimbuild = "ui_kittiko_bag"
    --Keep exe from complaining about no anim in log file for now
    inst.components.container.widgetanimbank = "ui_backpack_2x4"
    inst.components.container.widgetanimbuild = "ui_backpack_2x4"
	--Use image background until animation file can be made
	inst.components.container.widgetbgimage = "mods/Kittiko Bag/images/ui_kittiko_bag_7x3.tex"
	--inst.components.container.widgetbgimagepos = Vector3(-150, -50, 0)
	inst.components.container.widgetbgimagepos = Vector3(-65, -185, 0)
	
    inst.components.container.onopenfn = OnOpen
    inst.components.container.onclosefn = OnClose

	inst.OnSave = OnSave
	--inst.OnLoad = OnLoad
	inst.OnLoadPostPass = OnLoadPostPass
	
	return inst
end



return Prefab( "common/inventory/kittiko_bag", fn, assets)
